package com.chinawlx.wlxteacher.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者:吴名
 * 时间:16/9/9
 * 描述:显示加载(菊花旋转)dialog
 */
public class WLXLoadingFragment extends DialogFragment {

    @BindView(R.id.iv_loading)
    ImageView mIvLoading;

    private AnimationDrawable mLoadingAnim;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置dialog的背景色
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//去除外部区域半透明遮罩

        getDialog().setCanceledOnTouchOutside(false);//点击dialog外部不消失
        //点击返回键，不会消失
//        setCancelable(false);

        View view = inflater.inflate(R.layout.loading_dialog, container);
        ButterKnife.bind(this, view);

        //加载动画资源
        mLoadingAnim = (AnimationDrawable) mIvLoading.getDrawable();
        return view;
    }

    public static WLXLoadingFragment getInstance() {
        final WLXLoadingFragment loadingFragment = new WLXLoadingFragment();
        return loadingFragment;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mLoadingAnim != null) {
            mLoadingAnim.stop();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mLoadingAnim != null) {
            mLoadingAnim.start();
        }
    }
}
